﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class book_in : Form
    {
        public MySqlConnection conn = new DBclass().getConn();
        DataSet ds = new DataSet();
        DataTable dt;
        public book_in()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string fileName = fd.FileName;
                bind(fileName);
            }
        }
        private void bind(string fileName)
        {

            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                 "Data Source=" + fileName + ";" +
                 "Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'";
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
            DataSet ds = new DataSet();
            try
            {
                da.Fill(ds);
                dt = ds.Tables[0];
                this.dataGridView1.DataSource = dt;
            }
            catch (Exception err)
            {
                MessageBox.Show("操作失败！" + err.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            conn.Open();
            if (dataGridView1.Rows.Count > 0)
            {
                DataRow dr = null;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dr = dt.Rows[i];

                    insertToSql(dr);
                    
                }

                conn.Close();
                MessageBox.Show("导入成功！");
            }
            else
            {
                MessageBox.Show("没有数据！");
            }
        }
        private void insertToSql(DataRow dr)
        {
            //excel表中的列名和数据库中的列名一定要对应  
            string bID = dr["图书编号"].ToString().Trim();
            string ISBN = dr["ISBN"].ToString();
            string borrowed_whether = dr["是否已借"].ToString();
            string booked_whether = dr["是否预定"].ToString();
            string on_whether = dr["上下架状态"].ToString();
            string borrow_whether = dr["是否可外借"].ToString();
            string lose_whether = dr["是否丢失"].ToString();

            string sql = "insert into book_info values('" + bID + "','" + ISBN + "','" + borrowed_whether + "','" + booked_whether + "','" + on_whether + "','" + borrow_whether + "','" + lose_whether + "')";
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            cmd.ExecuteNonQuery();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            book1_in f = new book1_in();
            f.Show();
            this.Close();
        }
    }
}
